package com.wanmait.translate.system.service.impl;

import com.wanmait.translate.dto.ResultData;
import com.wanmait.translate.pojo.SystemDept;
import com.wanmait.translate.pojo.SystemDict;
import com.wanmait.translate.system.mapper.SystemDeptMapper;
import com.wanmait.translate.system.mapper.SystemDictMapper;
import com.wanmait.translate.system.mapper.SystemPostMapper;
import com.wanmait.translate.system.service.SystemDeptService;
import com.wanmait.translate.utils.DictMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SystemDeptServiceImpl  implements SystemDeptService {
    @Resource
    private SystemDeptMapper systemDeptMapper;
    @Resource
    private SystemDictMapper systemDictMapper;
    @Resource
    private SystemPostMapper systemPostMapper;
    @Override
    public List<SystemDept> listSystemDept() {
        SystemDept rootSystemDept = this.getRootSystemDept();
        List<SystemDept> systemDepts = systemDeptMapper.listSystemDept();
        systemDepts.add(rootSystemDept);
        return systemDepts;
    }
    /*
     * @Author huweibin
     * @Description 根据部门id获得当前部门信息()
     * @Date 16:01 2020/11/7
     **/
    @Override
    public SystemDept getByPrimaryKey(Integer id) {
        if(id==0){
            return this.getRootSystemDept();
        }else{
            return systemDeptMapper.getByPrimaryKey(id);
        }
    }
    /*
     * @Author huweibin
     * @Description 根据部门id获得当前部门和父部门信息
     * @Date 16:01 2020/11/7
     **/
    @Override
    public SystemDept getSelfAndParentByPrimaryKey(Integer id) {
        //获得当前部门信息
        SystemDept systemDept = systemDeptMapper.getByPrimaryKey(id);
        SystemDept parent=null;
        //如果父部门id为0，从字典中取公司信息
        if (systemDept.getParentId()==0){
            parent=this.getRootSystemDept();
        }else {
            ///如果父部门id不为0，从部门表中继续查
            parent=systemDeptMapper.getByPrimaryKey(systemDept.getParentId());
        }
        systemDept.setParent(parent);
        return systemDept;
    }

        /*
         * @Author huweibin
         * @Description 从字典中获得根部门(公司)信息
         * @Date 16:20 2020/11/7
         **/
        @Override
        public SystemDept getRootSystemDept(){
            SystemDept parent=new SystemDept();
            List<SystemDict> byDictTypeKey = systemDictMapper.getByDictTypeKey(DictMessage.DICT_COMPANT_NAME);
            SystemDict dic=byDictTypeKey.size()==0?null:byDictTypeKey.get(0);
            parent.setId(0);
            parent.setName(dic.getDictValue());
            return parent;
        }
    /*
     * @Author huweibin
     * @Description 创建保存新部门
     * @Date 15:42 2020/11/8
     **/
    @Override
    public void save(SystemDept systemDept) {
        systemDeptMapper.save(systemDept);
    }
    /*
     * @Author huweibin
     * @Description 更新部门
     * @Date 15:42 2020/11/8
     **/
    @Override
    public void update(SystemDept systemDept) {
        //更新之前的对象
        SystemDept before = systemDeptMapper.getByPrimaryKey(systemDept.getId());
        //获得原先所有的子节点,更新所有子节点的父节点
        List<Integer> childDept = systemDeptMapper.getChildDept(systemDept.getId());
        SystemDept systemMenu = new SystemDept();
        for (Integer id : childDept) {
            //设置原先的父节点为子节点的父节点
            systemMenu.setParentId(before.getParentId());
            systemMenu.setId(id);
            systemDeptMapper.update(systemMenu);
        }
        systemDeptMapper.update(systemDept);
    }
    /*
     * @Author huweibin
     * @Description 上传
     * @Date 17:14 2020/11/8
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData remove(Integer id) {
        List<Integer> list=systemDeptMapper.getChildDept(id);
        if (list.size()==0){
            systemDeptMapper.remove(id);
            return ResultData.successsWithOutData();
        }
        return ResultData.fail("删除父部门前，必须清空子部门");
    }

    @Override
    public Object find(String deptName, Integer enable) {

        return systemDeptMapper.find( deptName,  enable);
    }


}
